package com.atguigu.tingshu.album.api;

import com.atguigu.tingshu.album.service.TrackInfoService;
import com.atguigu.tingshu.common.login.TsLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.atguigu.tingshu.query.album.TrackInfoQuery;
import com.atguigu.tingshu.vo.album.AlbumTrackListVo;
import com.atguigu.tingshu.vo.album.TrackInfoVo;
import com.atguigu.tingshu.vo.album.TrackListVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

@Tag(name = "声音管理")
@RestController
@RequestMapping("api/album/trackInfo")
@SuppressWarnings({"all"})
public class TrackInfoApiController {

    @Autowired
    private TrackInfoService trackInfoService;

    /**
     * 上传声音
     *
     * @param file
     * @return
     */
    @Operation(summary = "上传声音")
    @PostMapping("/uploadTrack")
    public Result uploadTrack(MultipartFile file) {
        Map<String, Object> uploadTrack = trackInfoService.uploadTrack(file);
        return Result.ok(uploadTrack);
    }

    /**
     * 新增声音
     *
     * @param trackInfoVo
     * @return
     */
    @TsLogin
    @Operation(summary = "新增声音")
    @PostMapping("/saveTrackInfo")
    public Result saveTrackInfo(@RequestBody TrackInfoVo trackInfoVo) {
        Long userId = AuthContextHolder.getUserId() == null ? 1L : AuthContextHolder.getUserId();
        trackInfoService.saveTrackInfo(userId, trackInfoVo);
        return Result.ok();
    }

    /**
     * 查询当前用户声音分页列表
     *
     * @param pageNum
     * @param pageSize
     * @param trackInfoQuery
     * @return
     */
    @TsLogin
    @Operation(summary = "查询专辑声音分页列表")
    @PostMapping("/findUserTrackPage/{pageNum}/{pageSize}")
    public Result findAlbumTrackPage(@PathVariable Long pageNum,
                                     @PathVariable Long pageSize,
                                     @RequestBody TrackInfoQuery trackInfoQuery) {
        Long userId = AuthContextHolder.getUserId() == null ? 1L : AuthContextHolder.getUserId();
        trackInfoQuery.setUserId(userId);
        Page<TrackListVo> trackListVoPage = new Page<>(pageNum, pageSize);
        trackInfoService.findAlbumTrackPage(trackListVoPage, trackInfoQuery);
        return Result.ok(trackListVoPage);
    }

    /**
     * 删除声音
     *
     * @param id
     * @return
     */
    @Operation(summary = "删除声音")
    @DeleteMapping("removeTrackInfo/{id}")
    public Result removeTrackInfo(@PathVariable Long id) {
        //思路
        //1.track_info,track_stat：对这两张表要进行逻辑删除
        //2.修改album_info 表中的专辑声音总数
        //3.删除云点播中的声音记录
        trackInfoService.removeTrackInfo(id);
        return Result.ok();
    }

    /**
     * 根据id获取声音信息
     *
     * @param id
     * @return
     */
    @Operation(summary = "根据id获取声音信息")
    @GetMapping("/getTrackInfo/{id}")
    public Result getTrackInfo(@PathVariable Long id) {
        TrackInfo trackInfo = trackInfoService.getById(id);
        return Result.ok(trackInfo);
    }

    /**
     * 修改声音信息
     *
     * @param id
     * @param trackInfoVo
     * @return
     */
    @Operation(summary = "修改声音信息")
    @PutMapping("/updateTrackInfo/{id}")
    public Result updateTrackInfo(@PathVariable Long id, @RequestBody TrackInfoVo trackInfoVo) {
        trackInfoService.updateTrackInfo(id, trackInfoVo);
        return Result.ok();
    }

    /**
     * 获取专辑声音列表
     *
     * @param albumId
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Operation(summary = "获取专辑声音列表")
    @GetMapping("findAlbumTrackPage/{albumId}/{pageNo}/{pageSize}")
    public Result findAlbumTrackPage(@PathVariable Long albumId,
                                     @PathVariable Long pageNo,
                                     @PathVariable Long pageSize) {
        Long userId = AuthContextHolder.getUserId();
        Page<AlbumTrackListVo> trackListVoPage = new Page<>(pageNo, pageSize);
        trackInfoService.getAlbumTrackPage(trackListVoPage, albumId, userId);
        return Result.ok(trackListVoPage);
    }

    /**
     * 获取用户声音分集购买支付列表
     *
     * @param trackId
     * @return
     */
    @TsLogin
    @Operation(summary = "获取用户声音分集购买支付列表")
    @GetMapping("/findUserTrackPaidList/{trackId}")
    public Result<List<Map<String, Object>>> findUserTrackPaidList(@PathVariable Long trackId) {
        List<Map<String, Object>> mapList = trackInfoService.findUserTrackPaidList(trackId);
        return Result.ok(mapList);
    }

    /**
     * 批量获取下单付费声音列表
     * @param trackId
     * @param trackCount
     * @return
     */
    @GetMapping("/findPaidTrackInfoList/{trackId}/{trackCount}")
    Result<List<TrackInfo>> findPaidTrackInfoList(@PathVariable("trackId") Long trackId, @PathVariable("trackCount") Integer trackCount){
        List<TrackInfo> trackInfoList = trackInfoService.findPaidTrackInfoList(trackId, trackCount);
        return Result.ok(trackInfoList);
    }



}

